<extend name="Public/common" />
<block name="style">
    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor1_4_3-utf8-php/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor1_4_3-utf8-php/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor1_4_3-utf8-php/lang/zh-cn/zh-cn.js"></script>
    <script type="text/javascript" src="__PUBLIC__/qiniu/plupload/plupload.full.min.js"></script>
    <script type="text/javascript" src="__PUBLIC__/qiniu//plupload/i18n/zh_CN.js"></script>
    <script type="text/javascript" src="__PUBLIC__/qiniu/qiniu.js"></script>
</block>
<block name="body">
    <div class="document" style="padding-top: 50px">
        <div class="am-g am-container">
            <div class="am-u-lg-9">
                <form id="article-form" class="am-form am-form-horizontal" method="post" action="{:U('Home/Article/doAdd')}" enctype="multipart/form-data">
                    <div class="am-form-group">
                        <label for="title" class="am-u-lg-2 am-form-label">标题：</label>
                        <div class="am-u-lg-10">
                            <input name="title" maxlength="50" required type="text" id="title" placeholder="50字以内">
                        </div>
                    </div>
                    <div class="am-form-group">
                        <label for="category_id" class="am-u-lg-2 am-form-label">类型：</label>
                        <div class="am-u-lg-10">
                            <select id="category_id" name="category_id" required>
                                <volist name="_category_list" id="cvo">
                                    <option value="{$cvo['category_id']}">{$cvo['title']}</option>
                                </volist>
                            </select>
                        </div>
                    </div>
                    <div class="am-form-group">
                        <label for="des" class="am-u-lg-2 am-form-label">简介：</label>
                        <div class="am-u-lg-10">
                            <textarea name="des" maxlength="200" required id="des" placeholder="200字以内简介"></textarea>
                        </div>
                    </div>
                    <div class="am-form-group">
                        <label for="editor" class="am-u-lg-2 am-form-label">内容：</label>
                        <div class="am-u-lg-10">
                            <script id="editor" type="text/plain" style="width:100%;"></script>
                        </div>
                    </div>
                    <div class="am-form-group">
                        <label for="picture" class="am-u-lg-2 am-form-label">封面：</label>
                        <input type="hidden" id="picture" name="picture" value="">
                        <div class="am-u-lg-10">
                            <div id="container" style="padding-top: 5px">
                                <button class="am-btn am-btn-danger" id="pickfiles" type="button">点击上传</button>
                            </div>
                            <br>
                        </div>
                    </div>
                    <div class="am-form-group">
                        <label for="src_link" class="am-u-lg-2 am-form-label">来源：</label>
                        <div class="am-u-lg-10">
                            <input name="src_link" type="url" id="src_link" placeholder="原文链接，原创可以不填写">
                        </div>
                    </div>
                    <div class="am-form-group">
                        <div class="am-u-lg-10 am-u-lg-offset-2">
                            <button type="submit" class="am-btn am-btn-default">提交</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="am-u-lg-3">
                <div>
                    <img id="head-pic" src="__PUBLIC__/Home/images/default.jpeg" width="100%">
                </div>
            </div>
        </div>
    </div>
</block>
<block name="script">
    <script>
        highlight_subnav("{:U('Home/Article/index')}");
        var ue = UE.getEditor('editor');
        var url;
        var loading;
        $(function() {
            var ajax_form_options = {
//                target:        '#comment-form',   // target element(s) to be updated with server response
                beforeSubmit:  beforeSubmit,  // pre-submit callback
                success:       showResponse  // post-submit callback
            };
            $('#article-form').ajaxForm(ajax_form_options);
            function beforeSubmit(){
                loading = layer.load();
            }
            function showResponse(responseText,statusText){
                layer.close(loading);
                if(responseText.status == 1){
                    layer.msg(responseText.info,{icon: 1});
                    url = responseText.url;
                    setTimeout("window.location.href=url",1500);
                }else{
                    layer.msg(responseText.info);
                }
            }
            var domain = "<php> $qc = C('qiniu_config');echo $qc['url'] </php>";
            var uploader = Qiniu.uploader({
                runtimes: 'html5,flash,html4',    //上传模式,依次退化
                browse_button: 'pickfiles',       //上传选择的点选按钮，**必需**
                uptoken_url: "{:U('Home/Article/get_qiniu_token')}",
                //Ajax请求upToken的Url，**强烈建议设置**（服务端提供）
                // uptoken : '<Your upload token>',
                //若未指定uptoken_url,则必须指定 uptoken ,uptoken由其他程序生成
//                unique_names: true,
                // 默认 false，key为文件名。若开启该选项，SDK会为每个文件自动生成key（文件名）
                save_key: true,
                // 默认 false。若在服务端生成uptoken的上传策略中指定了 `sava_key`，则开启，SDK在前端将不对key进行任何处理
                domain: domain,
                //bucket 域名，下载资源时用到，**必需**
                container: 'container',           //上传区域DOM ID，默认是browser_button的父元素，
                max_file_size: '100mb',           //最大文件体积限制
                flash_swf_url: '__PUBLIC__/qiniu/plupload/Moxie.swf',  //引入flash,相对路径
                max_retries: 3,                   //上传失败最大重试次数
                dragdrop: true,                   //开启可拖曳上传
                drop_element: 'container',        //拖曳上传区域元素的ID，拖曳文件或文件夹后可触发上传
                chunk_size: '4mb',                //分块上传时，每片的体积
                auto_start: true,                 //选择文件后自动上传，若关闭需要自己绑定事件触发上传
                init: {
                    'FilesAdded': function(up, files) {
                        plupload.each(files, function(file) {
                            // 文件添加进队列后,处理相关的事情
                        });
                    },
                    'BeforeUpload': function(up, file) {
                        // 每个文件上传前,处理相关的事情
                        loading = layer.load();
                    },
                    'UploadProgress': function(up, file) {

                    },
                    'FileUploaded': function(up, file, info) {
                        var obj = JSON.parse(info);
                        $('#picture').val(obj.key);
                        $('#head-pic').attr('src',domain + obj.key);
                        layer.close(loading);
                    },
                    'Error': function(up, err, errTip) {
                        //上传出错时,处理相关的事情
                    },
                    'UploadComplete': function() {
                        //队列文件处理完毕后,处理相关的事情
                    },
                    'Key': function(up, file) {
                        // 若想在前端对每个文件的key进行个性化处理，可以配置该函数
                        // 该配置必须要在 unique_names: false , save_key: false 时才生效
                        var key = "";
                        // do something with key here
                        return key
                    }
                }
            });
        });
    </script>
</block>